package cn.tonyandmoney.voicenotes.ui.fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.support.annotation.Nullable;
import android.view.View;

import cn.tonyandmoney.voicenotes.R;

/**
 * 快速保存设置信息
 */
public class SettingFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener {
    private final String mTag = "SettingFragment";
    private SharedPreferences mPreference;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.setting);
        mPreference = getPreferenceManager().getSharedPreferences();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String key = getString(R.string.record_max_length);
        bindSummary(key, "5");
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        preference.setSummary(newValue.toString());
        return true;
    }


    private void bindSummary(String key, String defValue) {
        Preference preference = findPreference(key);
        if (preference == null) return;
        preference.setSummary(mPreference.getString(key, defValue));
        preference.setOnPreferenceChangeListener(this);
    }

}
